//
//  S1mpl3HotListenViewController.m
//  沙哈拉FM
//
//  Created by dllo on 15/11/7.
//  Copyright © 2015年 S1mpl3. All rights reserved.
//

#import "S1mpl3HotListenViewController.h"
#import "TwoToOne.h"
#import "TabBarController.h"
#import "FitHeader.h"
#import "S1mpl3Album.h"
#import "S1mpl3HotListenCollectionViewCell.h"
#import "S1mpl3CollectionReusableView.h"
#import "S1mpl3RecommendListenCollectionViewCell.h"
#import "S1mpl3AlbumViewController.h"
#import "S1mpl3HotListenHeadCollectionViewCell.h"
#import "S1mpl3HotListenHead.h"
#import "LZPlayViewController.h"
#import "S1mpl3MoreRecommend.h"


@interface S1mpl3HotListenViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong)UICollectionView *recommendCollectionView;
@property(nonatomic,strong)NSMutableArray *dataSourceArray;
@property(nonatomic,assign)NSInteger page;
@property(nonatomic,strong)S1mpl3HotListenHead *hotListenHead;
@property(nonatomic,strong)NSMutableArray *LZarray;
@end

@implementation S1mpl3HotListenViewController

-(void)getData{
    [TwoToOne getDataByString:[NSString stringWithFormat:@"http://mobile.ximalaya.com/m/subject_detail?device=android&id=%@", _specialID] BodyString:nil WithDataBlock:^(id data) {
        _hotListenHead = [[S1mpl3HotListenHead alloc] init];
        [_hotListenHead setValuesForKeysWithDictionary:[data objectForKey:@"info"]];
        for (NSDictionary *dic in [data objectForKey:@"list"]) {
            if ([_contentType integerValue] == 2) {
                LZBillboardModel *model = [[LZBillboardModel alloc] init];
                
                [model setValuesForKeysWithDictionary:dic];
                [_LZarray addObject:model];
                [_dataSourceArray addObject:dic];
            }else{
                /*
                 S1mpl3Album *album = [[S1mpl3Album alloc] init];
                 [album setValuesForKeysWithDictionary:dic];
                 [_dataSourceArray addObject:album];
                 */
                S1mpl3MoreRecommend *moreRecommend = [[S1mpl3MoreRecommend alloc] init];
                [moreRecommend setValuesForKeysWithDictionary:dic];
                [_dataSourceArray addObject:moreRecommend];
            }
        }
        [_recommendCollectionView reloadData];
    }];
}

-(void)viewWillAppear:(BOOL)animated{
    [_recommendCollectionView reloadData];
    [[TabBarController shareInstance] remainButtonOfPlay:YES];
}

-(void)viewWillDisappear:(BOOL)animated{
    [[TabBarController shareInstance] remainButtonOfPlay:NO];
}

-(void)loadView{
    [super loadView];
    _dataSourceArray = [NSMutableArray array];
    _LZarray = [NSMutableArray array];
    [self getData];
    self.view.backgroundColor = [UIColor colorWithRed:0.93 green:0.92 blue:0.91 alpha:1];
    UICollectionViewFlowLayout *contentFlowlayout = [[UICollectionViewFlowLayout alloc] init];
    contentFlowlayout.minimumInteritemSpacing = 0;
    contentFlowlayout.minimumLineSpacing = 0;
    contentFlowlayout.sectionInset = UIEdgeInsetsMake(0, 0, 10 * FITHeight, 0);
    contentFlowlayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    _recommendCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 375 * FITWidth, 675 * FITHeight - 64) collectionViewLayout:contentFlowlayout];
    _recommendCollectionView.dataSource = self;
    _recommendCollectionView.delegate = self;
    _recommendCollectionView.showsVerticalScrollIndicator = NO;
    _recommendCollectionView.backgroundColor = [UIColor colorWithRed:0.93 green:0.92 blue:0.91 alpha:1];
    [self.view addSubview:_recommendCollectionView];
    
    //Secion注册
    [_recommendCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];
    
    
    [_recommendCollectionView registerClass:[S1mpl3CollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head1"];
    //精品听单注册
    if ([_contentType integerValue] == 2) {
        [_recommendCollectionView registerClass:[S1mpl3HotListenCollectionViewCell class] forCellWithReuseIdentifier:@"specialIdentifier"];
    }else if ([_contentType integerValue] == 1){
        [_recommendCollectionView registerClass:[S1mpl3RecommendListenCollectionViewCell class] forCellWithReuseIdentifier:@"specialIdentifier"];
    }
    
    [_recommendCollectionView registerClass:[S1mpl3HotListenHeadCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
}

#pragma mark -- CollectionView代理方法
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if ([_contentType integerValue] == 2) {
        if (indexPath.section == 1) {
            S1mpl3HotListenCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"specialIdentifier" forIndexPath:indexPath];
            //            cell.backgroundColor = [UIColor whiteColor];
            if (_dataSourceArray.count != 0) {
                cell.videoDic = [_dataSourceArray objectAtIndex:indexPath.row];
            }
            return cell;
        }else{
            S1mpl3HotListenHeadCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
            cell.hotListenHead = _hotListenHead;
            //            cell.backgroundColor = [UIColor whiteColor];
            return cell;
        }
    }else{
        if (indexPath.section == 1) {
            S1mpl3RecommendListenCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"specialIdentifier" forIndexPath:indexPath];
            //            cell.backgroundColor = [UIColor whiteColor];
            if (_dataSourceArray.count != 0) {
                cell.moreRecommend = [_dataSourceArray objectAtIndex:indexPath.row];
            }
            return cell;
        }else{
            S1mpl3HotListenHeadCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
            //            cell.backgroundColor = [UIColor whiteColor];
            cell.hotListenHead = _hotListenHead;
            return cell;
        }
    }
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        
        
        return CGSizeMake(self.view.frame.size.width, 0);
    }
    return CGSizeMake(self.view.frame.size.width, 30 * FITHeight);
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head" forIndexPath:indexPath];
        return view;
    }else{
        S1mpl3CollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head1" forIndexPath:indexPath];
        view.label.text = @"听单推荐";
        return view;
    }
    
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 2;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    //    return [[_dataSourceDic objectForKey:[_sectionArray objectAtIndex:section]] count];
    if (section == 0) {
        return 1;
    }else
        return _dataSourceArray.count;
    
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        CGRect rect = [_hotListenHead.intro boundingRectWithSize:CGSizeMake(355 * FITWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16], NSFontAttributeName, nil] context:nil];
        return CGSizeMake(375 * FITWidth, ceill(rect.size.height + 1) + 110 * FITHeight);
    }else
        return CGSizeMake(375 * FITWidth, 130 * FITHeight);
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (_dataSourceArray.count == 0) {
        return;
    }
    if ([_contentType integerValue] == 2) {
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[LZPlayViewController shareInstance]];
        [LZPlayViewController shareInstance].dataArray = _LZarray;
        [LZPlayViewController shareInstance].dicArray = _dataSourceArray;
        [LZPlayViewController shareInstance].index = indexPath.row;
        [LZPlayViewController shareInstance].dic = _dataSourceArray[indexPath.row];
        [[NSUserDefaults standardUserDefaults] setInteger:1 forKey:@"cell0"];
        [self presentViewController:nav animated:YES completion:nil];
    }else{
        S1mpl3AlbumViewController *albumVC = [[S1mpl3AlbumViewController alloc] init];
        
        albumVC.albumId = [[_dataSourceArray objectAtIndex:indexPath.row] ID];
        albumVC.myTitle = [[_dataSourceArray objectAtIndex:indexPath.row] title];
        albumVC.moreRecommend = [_dataSourceArray objectAtIndex:indexPath.row];
        [self.navigationController pushViewController:albumVC animated:YES];
    }
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"听单";
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
